﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HousingInfo.Library;

namespace WebHousingInfo.Controls
{
    public partial class DNhap : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btllogin_Click(object sender, EventArgs e)
        {
            string userName = txtUserName.Text.ToLower();
            string passWord = txtPassword.Text;
            if (UserBO.CheckUserNameExist(userName))
            {
                var userBo = UserBO.GetUserBO(userName);
                if (userBo.Islock == false)
                {
                    if (userBo.UserName == userName && userBo.Password == UserBO.encryptData(passWord))
                    {
                        Session["UserName"] = userBo.UserName;
                        Session["RoleUser"] = userBo.RoleID;
                        Session["PassWord"] = userBo.Password;
                        checkSavePassWithCookie();
                        checkCookie();
//                        Response.Redirect("Home.aspx");
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert('Tài khoản hoặc mật khẩu không đúng !')", true);
                    }
                }
                else
                {
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert('Tài khoản này đang bị khoá !')", true);
                }
            }
            else
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert('Tài khoản hoặc mật khẩu không đúng !')", true);
            }            
        }

        private void checkSavePassWithCookie()
        {
            if (chkLuumatkhau.Checked)
            {
                //FormsAuthentication.SetAuthCookie(ApplicationContext.User.Identity.Name, true);
                var userName = new HttpCookie("userName", txtUserName.Text) { Expires = DateTime.Now.AddDays(30) };
                var passWord = new HttpCookie("passWord", txtPassword.Text) { Expires = DateTime.Now.AddDays(30) };
                Response.Cookies.Add(userName);
                Response.Cookies.Add(passWord);
            }
            else
            {
                var userName = new HttpCookie("userName", null) { Expires = DateTime.Now };
                var passWord = new HttpCookie("passWord", null) { Expires = DateTime.Now };
                Response.Cookies.Add(userName);
                Response.Cookies.Add(passWord);
            }
        }

        private void checkCookie()
        {
            //if (Request.Cookies["userName"] != null && Request.Cookies["passWord"] != null)
            //{
            //    Session["UserName"] = Request.Cookies["userName"].Value;
            //    Session["PassWord"] = Request.Cookies["passWord"].Value;
            //}
        }

        protected void txtPassword_TextChanged(object sender, EventArgs e)
        {
            btllogin.Focus();
        }

        protected void txtUserName_TextChanged(object sender, EventArgs e)
        {
            btllogin.Focus();
        }
    }
}